package com.ruoyi.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.TRegisterInfo;
import com.ruoyi.business.service.ITRegisterInfoService;
import com.ruoyi.business.service.ITResourceInfoService;
import com.ruoyi.business.vo.TResourceInfoVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 资源中心Controller
 *
 * @author Tellsea
 * @date 2024-01-17
 */
@Api(tags = "资源中心Controller")
@RestController
@RequestMapping("/business/resourceInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TResourceInfoController extends BaseController {

    private final ITResourceInfoService tResourceInfoService;
    private final ITRegisterInfoService registerInfoService;

    @ApiOperation("查询资源中心列表")
    //@PreAuthorize("@ss.hasPermi('business:resourceInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<TResourceInfoVo> list(TResourceInfoVo entity) {
        //当前登录人员最新报名数据为报到状态时返回当前数据
        Long userId = SecurityUtils.getLoginUser().getUserId();
        QueryWrapper<TRegisterInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("notice_status",3).orderByDesc("create_time");
        queryWrapper.last(" limit 1");
        List<TRegisterInfo> list = registerInfoService.list(queryWrapper);
        if (list != null && list.size() > 0){
            return tResourceInfoService.queryList(entity);
        }
        return null;
    }

    @ApiOperation("查询资源中心所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TResourceInfoVo entity) {
        return AjaxResult.success("查询成功", tResourceInfoService.queryAll(entity));
    }

    @ApiOperation("导出资源中心列表")
    @PreAuthorize("@ss.hasPermi('business:resourceInfo:export')")
    @Log(title = "资源中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TResourceInfoVo entity) {
        List<TResourceInfoVo> list = tResourceInfoService.queryAll(entity);
        ExcelUtil<TResourceInfoVo> util = new ExcelUtil<>(TResourceInfoVo.class);
        util.exportExcel(response, list, "资源中心数据");
    }

    @ApiOperation("获取资源中心详细信息")
    //@PreAuthorize("@ss.hasPermi('business:resourceInfo:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TResourceInfoVo entity = tResourceInfoService.queryById(id);
        if (Objects.isNull(entity.getReadNumber())) {
            entity.setReadNumber(0L);
        } else {
            entity.setReadNumber(entity.getReadNumber() + 1);
        }
        tResourceInfoService.updateById(entity);
        return AjaxResult.success("查询成功", entity);
    }

    @ApiOperation("新增资源中心")
    @PreAuthorize("@ss.hasPermi('business:resourceInfo:add')")
    @Log(title = "资源中心", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TResourceInfoVo entity) {
        entity.setReadNumber(0L);
        return toAjax(tResourceInfoService.save(entity));
    }

    @ApiOperation("修改资源中心")
    @PreAuthorize("@ss.hasPermi('business:resourceInfo:edit')")
    @Log(title = "资源中心", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TResourceInfoVo entity) {
        return toAjax(tResourceInfoService.updateById(entity));
    }

    @ApiOperation("删除资源中心")
    @PreAuthorize("@ss.hasPermi('business:resourceInfo:remove')")
    @Log(title = "资源中心", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tResourceInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
